﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace OpenComposite.Workflow
{
	public partial class ActivityTextBox: Form
	{
		public ActivityTextBox()
		{
			InitializeComponent();
		}

		public ActivityTextBox(string text, Point location)
		{
			InitializeComponent();
			//TopLevel = false;
			this.Location = location;
			this.textBox.Text = text;
			this.DialogResult = DialogResult.None;
		}

		public string TextBoxText
		{
			get { return textBox.Text; }
			internal set { textBox.Text = value; }
		}

		public void FocusTextBox()
		{
			textBox.Focus();
		}

		private void textBox_Leave(object sender, EventArgs e)
		{
			exit(true);
		}

		private void exit(bool cancel)
		{
			if ( cancel ) this.DialogResult = DialogResult.Cancel;
			else this.DialogResult = DialogResult.OK;
			this.Close();
			//this.Dispose();
		}

		private void ActivityTextBox_Leave(object sender, EventArgs e)
		{
			exit(true);
		}

		private void textBox_KeyPress(object sender, KeyPressEventArgs e)
		{
			// 10 - Ctrl + Enter
			// 27 - Esc
			if ( e.KeyChar == (char)10 ) {
				e.Handled = true;
				exit(false);
			} else if ( e.KeyChar == (char)27 ) {
				e.Handled = true;
				exit(true);
			}
		}

		private void ActivityTextBox_Deactivate(object sender, EventArgs e)
		{
			if ( this.DialogResult != DialogResult.OK ) {
				exit(true);
			}
		}

		private void ActivityTextBox_Shown(object sender, EventArgs e)
		{
			textBox.Focus();
			textBox.SelectAll();
			ToolTip toolTip = new ToolTip();
			toolTip.IsBalloon = true;
			toolTip.ShowAlways = true;
			toolTip.Show(
				string.Format("Cancel with Esc{0}Accept with Ctrl+Return",Environment.NewLine),
				this, 0, -this.Height - 29);
		}

		private void ActivityTextBox_Activated(object sender, EventArgs e)
		{
		}
	}
}
